package com.ly.hotel.view.fragments.music;

import java.util.List;
import java.util.Vector;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;

import com.ly.hotel.R;
import com.ly.hotel.model.MusicBean;

/**
 * @author jouson
 * 正在播放的列表Adapter
 * 包含歌名和移除按钮
 */
public class MusicAlbumMusicListAdapter extends BaseAdapter {
	private MusicActivity mContext;
	private LayoutInflater mInflater;
	private List<MusicBean> musicAlbumMusicList;

	public MusicAlbumMusicListAdapter(MusicActivity c, List<MusicBean> musicAlbumMusicList) {
		this.musicAlbumMusicList = musicAlbumMusicList;
		mContext = c;
		mInflater = LayoutInflater.from(mContext);
	}

	@Override
	public int getCount() {
		return musicAlbumMusicList.size();
	}

	@Override
	public Object getItem(int postion) {
		return musicAlbumMusicList.get(postion);
	}

	@Override
	public long getItemId(int postion) {
		return postion;
	}

	@Override
	public View getView(int position, View contentView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		final int pos = position;
		AlbumMusicListViewHolder holder;
		
		if(musicAlbumMusicList == null){
			return null;
		}
		if (contentView == null) {

			holder = new AlbumMusicListViewHolder();
			contentView = mInflater.inflate(R.layout.music_album_music_list_item, null);
			holder.mbtn = (Button) contentView.findViewById(R.id.m_music_album_music_list_btn);
			
			holder.madd = (ImageButton) contentView.findViewById(R.id.m_music_album_music_list_add_btn);
			contentView.setTag(holder);
			
		} else {
			holder = (AlbumMusicListViewHolder) contentView.getTag();
		}
		
		holder.mbtn.setText(musicAlbumMusicList.get(position).getName());

		holder.madd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub

				MusicBean b = musicAlbumMusicList.get(pos);
				mContext.addMusicToPlayList(b);
			}
		});

		holder.mbtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				List<MusicBean> mb = new Vector<MusicBean>();
				mb.addAll(musicAlbumMusicList);
				mContext.addMusicToPlayList(mb,pos);
			}
		});
		
		return contentView;
	}
	
	static class AlbumMusicListViewHolder {
		Button mbtn;//歌名的按钮
		ImageButton madd;//添加按钮
	}
}
